package com.cnsoft.portal.saas.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cnsoft.framework.json.JSONObject;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.framework.utils.DataBaseBeanComparator;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.stereotype.Service;

import com.cnsoft.persistent.company2.C211020SupplierService.C211020SupplierServiceDBO;
import com.cnsoft.persistent.company2.C211020SupplierService.C211020SupplierServiceDao;

/**
 * 服务信息
 */
@Service("SaasAPPInfoCacheService")
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SaasAPPInfoCacheService extends MyServiceSupport implements SaasAppConstants {
	@Resource
	private C211020SupplierServiceDao daoC211020SupplierService;

	/**
	 * 获得服务信息
	 * 
	 * @param player
	 * @return
	 */
	public List<C211020SupplierServiceDBO> loadSupplierServices() throws Exception {
		// 获得缓存信息
		String key = SAAS_APP + CACHE_ALL;
		Map<Object, Object> cache = myCacheService.getAllObjectInMap(key);
		if (EmptyHelper.isEmpty(cache)) {
			return getSupplierServices();
		}

		myCacheService.expire(key, waitTimeSecond);
		return JSONObject.changeToArraySort(cache, C211020SupplierServiceDBO.class, new DataBaseBeanComparator<C211020SupplierServiceDBO>());
	}

	/**
	 * 获得分类服务信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public Map<String, List<C211020SupplierServiceDBO>> loadSupplierServiceAll() throws Exception {
		String key = SAAS_APP + CACHE_ALL;
		Map<String, List<C211020SupplierServiceDBO>> map = new HashMap<String, List<C211020SupplierServiceDBO>>();
		Map<Object, Object> cache = myCacheService.getAllObjectInMap(key);
		if (EmptyHelper.isEmpty(cache)) {
			List<C211020SupplierServiceDBO> list = getSupplierServices();
			if (EmptyHelper.isEmpty(cache)) {
				return map;
			}
			for (C211020SupplierServiceDBO item : list) {
				prepareSupplierServiceData(map, item);
			}
		} else {
			C211020SupplierServiceDBO item;
			for (Object content : cache.values()) {
				item = JSONObject.parseObject((String) content, C211020SupplierServiceDBO.class);
				prepareSupplierServiceData(map, item);
			}
		}
		
		return map;
	}

	/**
	 * 数据整理
	 * @param map
	 * @param item
	 * @throws Exception
	 */
	private void prepareSupplierServiceData(Map<String, List<C211020SupplierServiceDBO>> map, C211020SupplierServiceDBO item) throws Exception {
		List<C211020SupplierServiceDBO> list = map.get(item.getServiceTypeCode());
		if (EmptyHelper.isEmpty(list)) {
			list = new ArrayList<C211020SupplierServiceDBO>();
		}
		list.add(item);
		map.put(item.getServiceTypeCode(), list);
	}

	/**
	 * 获取某一个分类
	 * 
	 * @param serviceTypeCode
	 * @return
	 * @throws Exception
	 */
	public List<C211020SupplierServiceDBO> loadSupplierServices(String serviceTypeCode) throws Exception {
		// 获得缓存信息
		String key = SAAS_APP + CACHE_ALL;
		Map<Object, Object> cache = myCacheService.getAllObjectInMap(key);
		if (EmptyHelper.isEmpty(cache)) {
			return getSupplierServices();
		}

		myCacheService.expire(key, waitTimeSecond);
		return JSONObject.changeToArraySort(cache, C211020SupplierServiceDBO.class, new DataBaseBeanComparator<C211020SupplierServiceDBO>(), new AppServiceComparatorFilter(serviceTypeCode));
	}

	/**
	 * 获得系统提供服务信息
	 */
	private List<C211020SupplierServiceDBO> getSupplierServices() throws Exception {
		C211020SupplierServiceDBO param = new C211020SupplierServiceDBO();

		List<C211020SupplierServiceDBO> list = daoC211020SupplierService.doSelectData(param);

		if (EmptyHelper.isNotEmpty(list)) {
			String key = SAAS_APP + CACHE_ALL;
			for (C211020SupplierServiceDBO item : list) {
				myCacheService.addObjectInMap(key, item.getId(), JSONObject.toJsonString(item));
			}

			myCacheService.expire(key, waitTimeSecond);
		}

		return list;
	}

}
